package com.monna.games_2048;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.monna.entity.User;
import com.monna.util.UserUtil;

public class RegisterActivity extends AppCompatActivity {

    private String account;
    private String password;
    private String confirmPassowrd;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Button button = findViewById(R.id.bt_register);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                valEditText();
                User user = isSamePassword();
                register(user);
            }
        });
    }

    /**
     * 用户注册
     * @param user
     */
    public void register(User user){
        if (user != null){
            Boolean register = new UserUtil(this).userRegister(user);
            if (register){
                alertRegisterSuccess();
            }else {
                alertRegisterFalse();
            }
        }else {
            alertPaswordError();
        }
    }

    /**
     * 初始化控件值
     */
    private void valEditText(){
        EditText etAccount = findViewById(R.id.reval_account);
        account = etAccount.getText().toString().trim();
        EditText etPassword = findViewById(R.id.reval_password);
        password = etPassword.getText().toString().trim();
        EditText etConfirmPassword = findViewById(R.id.reval_confirm_password);
        confirmPassowrd = etConfirmPassword.getText().toString().trim();
        EditText etName = findViewById(R.id.reval_name);
        name = etName.getText().toString().trim();
    }

    /**
     * 判断两次密码输入是否相同
     * @return
     */
    private User isSamePassword(){
        User user = null;
        if (password.equals(confirmPassowrd)){
            user = new User(account, password, name);
        }
        return user;
    }

    /**
     * 弹出对话框提示用户两次密码不正确
     */
    private void alertPaswordError(){
        new AlertDialog.Builder(this)
                .setTitle("无效的密码")
                .setIcon(R.mipmap.ic_start2048)
                .setMessage("注册失败，两次密码输入不一致！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = getIntent();
                        finish();
                        startActivity(intent);
                    }
                }).show();
    }

    /**
     * 弹出对话框提示用户注册成功
     */
    private void alertRegisterSuccess(){
        new AlertDialog.Builder(this)
                .setTitle("注册成功")
                .setIcon(R.mipmap.ic_start2048)
                .setMessage("注册成功，请登录！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                }).show();
    }

    /**
     * 弹出对话框提示用户注册失败
     */
    private void alertRegisterFalse(){
        new AlertDialog.Builder(this)
                .setTitle("注册失败")
                .setIcon(R.mipmap.ic_start2048)
                .setMessage("注册失败，请重新注册！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = getIntent();
                        finish();
                        startActivity(intent);
                    }
                }).show();
    }
}
